package com.du.limiting.sentinel.runner;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.fastjson.JSONObject;
import com.du.limiting.sentinel.mapper.ParamFlowItemDao;
import com.du.limiting.sentinel.mapper.ParamFlowRuleDao;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author dujunguang
 * @date 2022/2/23 11:07
 * @description: TODO
 **/
//@Component
public class SentinelConfigRunnerBak implements CommandLineRunner {

    @Resource
    private ParamFlowRuleDao paramFlowRuleDao;

    @Resource
    private ParamFlowItemDao paramFlowItemDao;


    @Override
    public void run(String... args) throws Exception {

        // 流控规则-QPS
        flowControlQps();

        // 流控规则-threadNum
//        flowControlThreadNum();

        // 流控规则-来源方
        // OriginParser
//        flowControlLimitApp();

        // 热点规则
        hotPointInit();
    }

    private void flowControlLimitApp() {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("flowControlLimitApp");
        rule.setCount(8);
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setLimitApp("lisi");
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }

    private void flowControlThreadNum() {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("flowControlThreadNum");
        rule.setCount(5);
        rule.setGrade(RuleConstant.FLOW_GRADE_THREAD);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }

    private void flowControlQps() {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule1 = new FlowRule();
        rule1.setResource("hotPoint");
        rule1.setCount(5);
        rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
//        ClusterFlowConfig clusterConfig = new ClusterFlowConfig();

//        rule1.setClusterMode(true);
//        clusterConfig.setSampleCount(10);
//        clusterConfig.setThresholdType(ClusterRuleConstant.FLOW_THRESHOLD_GLOBAL);
////        clusterConfig.setWindowIntervalMs(2000);
//        clusterConfig.setFlowId(1L);
//        clusterConfig.setFallbackToLocalWhenFail(true);
//        clusterConfig.setStrategy(ClusterRuleConstant.FLOW_CLUSTER_STRATEGY_BORROW_REF);
//        rule1.setClusterConfig(clusterConfig);
//        rule1.setLimitApp("lisi");
        rules.add(rule1);
        FlowRuleManager.loadRules(rules);
        System.out.println(JSONObject.toJSONString(Collections.singletonList(rules)));
    }


    public void hotPointInit() {
        // 资源名
//        ParamFlowClusterConfig clusterConfig = new ParamFlowClusterConfig();
//        clusterConfig.setFallbackToLocalWhenFail(true);
//        clusterConfig.setFlowId(1L);
//        clusterConfig.setThresholdType(ClusterRuleConstant.FLOW_THRESHOLD_GLOBAL);

        ParamFlowRule rule = new ParamFlowRule("hotPoint")
                // 设置参数下标
                .setParamIdx(0)
                // qps - 默认模式
                .setGrade(RuleConstant.FLOW_GRADE_QPS)
                // 统计窗口时长
                .setDurationInSec(2)
                // 流控效果，默认为0，快速失败模式
                .setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT)
                .setCount(20);
//                .setClusterMode(true);
//                .setClusterConfig(clusterConfig);

        // 热点项设置
//        ParamFlowItem item = new ParamFlowItem()
//                .setObject("lisi")
//                .setClassType(String.class.getName())
//                .setCount(10);
//        rule.setParamFlowItemList(Collections.singletonList(item));
        ParamFlowRuleManager.loadRules(Collections.singletonList(rule));

        System.out.println(JSONObject.toJSONString(Collections.singletonList(rule)));

    }

}
